/* 输入  ABDEGCF(先序)
         DBGEACF(中序)
    输出 DGEBFCA(后序)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct treenode
{
    char data;
    struct treenode *left;
    struct treenode *right;
} *Tree, TreeNode;

bool isempty(char s[])
{
    return strlen(s) == 0;
}
char *target_left(char s[], char left[], char c)
{
    int len = strlen(s);
    int i = 0;
    for (i = 0; i < len; i++)
    {
        if (s[i] == c)
        {
            break;
        }
        left[i] = s[i];
    }
    left[i] = '\0';
    return left;
}
char *target_right(char s[], char right[], char c)
{
    int len = strlen(s);
    int i, k = 0;
    for (i = 0; i < len; i++)
    {
        if (s[i] == c)
        {
            break;
        }
    }
    for (int j = i + 1; j < len; j++)
    {
        right[k++] = s[j];
    }
    right[k] = '\0';
    return right;
}

char target_preElement(char s1[], char s2[])
{
    int len1 = strlen(s1);
    int len2 = strlen(s2);
    for (int i = 0; i < len1; i++)
    {
        for (int j = 0; j < len2; j++)
        {
            if (s1[i] == s2[j])
            {
                return s1[i];
            }
        }
    }
    return 0;
}

Tree origin_tree(Tree &T, char preOrder[], char inOrder[])
{
    if (isempty(preOrder))
    {
        return NULL;
    }
    T = (Tree)malloc(sizeof(TreeNode));
    T->data = preOrder[0];
    T->left = NULL;
    T->right = NULL;
    if (strlen(inOrder) == 1)
    {
        return T;
    }
    // 接下来对先序序列和中序序列进行划分

    // 根据首先添加的先序序列的第一个结点,对中序序列进行划分
    char left_inorder[100], right_inorder[100];
    target_left(inOrder, left_inorder, T->data);   // 左半部分
    target_right(inOrder, right_inorder, T->data); // 右半部分

    // 这里有个思想:当前假设left_inorder有k个,那么这串元素,位于先序序列第2个位置到k+1个位置
    int left_inorder_count = strlen(left_inorder);

    // 接下来对先序序列进行划分
    char left_preorder[100], right_preorder[100];
    strncpy(left_preorder, preOrder + 1, left_inorder_count);
    left_preorder[left_inorder_count] = '\0';
    strcpy(right_preorder, preOrder + 1 + left_inorder_count);

    // 递归子树
    T->left = origin_tree(T->left, left_preorder, left_inorder);
    T->right = origin_tree(T->right, right_preorder, right_inorder);
    return T;
}
void postOrder(Tree T)
{
    if (T == NULL)
    {
        return;
    }
    postOrder(T->left);
    postOrder(T->right);
    printf("%c", T->data);
}
int main()
{
    char preOrder[100], inOrder[100];
    scanf("%s%s", preOrder, inOrder);
    Tree T;
    origin_tree(T, preOrder, inOrder);
    postOrder(T);
}